package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.intellij.openapi.ui.ComboBox;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author synhard
 * @description 选择app的panel
 * @date 2024/5/23 13:35
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class AppSelectPanel extends JPanel {

    ComboBox<String> comboBox;

    private static final Logger logger = LoggerFactory.getLogger(AppSelectPanel.class);

    public AppSelectPanel() {
        this.setLayout(new FlowLayout());
        add(new JLabel("选择应用: "));
        // 创建一个 ComboBox 实例
        this.comboBox = new ComboBox<>();

        if (!StringUtils.isEmpty(GlobalConfig.appName)) {
            comboBox.removeAllItems();
            for (String appName : GlobalConfig.appNameList) {
                this.comboBox.addItem(appName);
            }
            this.comboBox.setSelectedItem(GlobalConfig.appName);
        }
        // 添加一个动作监听器，以打印用户选择的选项
        this.comboBox.addActionListener(e -> GlobalConfig.appName = (String) this.comboBox.getSelectedItem());

        add(this.comboBox);
    }

    // 新增方法用于动态刷新应用列表
    public void refreshAppList(List<String> apps) {
        comboBox.removeAllItems(); // 清空现有选项
        for (String app : apps) {
            comboBox.addItem(app);
        }
    }

}
